# Contributor: Simon Frankenberger <simon-alpine@fraho.eu>
# Maintainer: Simon Frankenberger <simon-alpine@fraho.eu>
pkgname=openjdk17
pkgver=17.0.13_p11
_pkgver=${pkgver%_p*}-ga
pkgrel=0
pkgdesc="Oracle OpenJDK 17"
provider_priority=17
url="https://github.com/openjdk/jdk17u"
# oracle dropped support for 32 bit
arch="all !x86 !armhf !armv7 !riscv64"
license="GPL-2.0-with-classpath-exception"
makedepends="
	$pkgname-bootstrap
	autoconf
	bash
	gawk
	grep
	make
	zip
	alsa-lib-dev
	cups-dev
	elfutils-dev
	fontconfig-dev
	freetype-dev
	giflib-dev
	lcms2-dev
	libffi-dev
	libjpeg-turbo-dev
	libx11-dev
	libxext-dev
	libxrandr-dev
	libxrender-dev
	libxt-dev
	libxtst-dev
	linux-headers
	zlib-dev
	"
depends="$pkgname-jdk $pkgname-demos $pkgname-doc" # for the virtual openjdk16 package
subpackages="$pkgname-jmods:_jmods:noarch
	$pkgname-demos:_demos:noarch
	$pkgname-doc:_doc:noarch
	$pkgname-jre:_jre
	$pkgname-src:_src:noarch
	$pkgname-static-libs:_static_libs
	$pkgname-jre-headless:_jre_headless
	$pkgname-jdk:_jdk
	"
source="jdk-$_pkgver.tar.gz::https://github.com/openjdk/jdk17u/archive/jdk-$_pkgver.tar.gz
	gtest-1.13.0.tar.gz::https://github.com/google/googletest/archive/v1.13.0.tar.gz
	ppc64le.patch
"
builddir="$srcdir/jdk17u-jdk-${_pkgver/+/-}"

provides="$pkgname-bootstrap=$pkgver-r$pkgrel"

_java_home="/usr/lib/jvm/java-17-openjdk"

ldpath="$_java_home/lib:$_java_home/lib/server"
sonameprefix="$pkgname:"

# enable running the JTReg tests in check?
# see comment in that function for explanation
_run_jtreg=${_run_jtreg:-0}
if [ $_run_jtreg -ne 0 ]; then
	makedepends="$makedepends java-jtreg"
	checkdepends="$checkdepends font-freefont xvfb-run"
fi

_jvm_variants=server
case $CTARGET_ARCH in
loongarch64)	_jvm_variants=zero	;;
*)		_jvm_variants=server	;;
esac

# secfixes:
#   17.0.13_p11-r0:
#     - CVE-2024-21235
#     - CVE-2024-21210
#     - CVE-2024-21211
#     - CVE-2024-21208
#     - CVE-2024-21217
#   17.0.12_p7-r0:
#     - CVE-2024-21147
#     - CVE-2024-21145
#     - CVE-2024-21140
#     - CVE-2024-21131
#     - CVE-2024-21138
#   17.0.11_p9-r0:
#     - CVE-2024-21892
#     - CVE-2024-20954
#     - CVE-2024-21098
#     - CVE-2024-21011
#     - CVE-2024-21068
#     - CVE-2024-21094
#     - CVE-2024-21012
#   17.0.10_p7-r0:
#     - CVE-2023-5072
#     - CVE-2024-20932
#     - CVE-2024-20918
#     - CVE-2024-20952
#     - CVE-2024-20919
#     - CVE-2024-20921
#     - CVE-2024-20926
#     - CVE-2024-20945
#     - CVE-2024-20955
#   17.0.9_p8-r0:
#     - CVE-2023-30589
#     - CVE-2023-22081
#     - CVE-2023-22025
#   17.0.8_p7-r0:
#     - CVE-2023-22041
#     - CVE-2023-25193
#     - CVE-2023-22044
#     - CVE-2023-22045
#     - CVE-2023-22049
#     - CVE-2023-22036
#     - CVE-2023-22006
#   17.0.7_p7-r0:
#     - CVE-2023-21930
#     - CVE-2023-21967
#     - CVE-2023-21954
#     - CVE-2023-21939
#     - CVE-2023-21938
#     - CVE-2023-21968
#     - CVE-2023-21937
#   17.0.6_p10-r0:
#     - CVE-2023-21835
#     - CVE-2023-21843
#   17.0.5_p8-r0:
#     - CVE-2022-21628
#     - CVE-2022-21618
#     - CVE-2022-39399
#     - CVE-2022-21624
#     - CVE-2022-21619
#   17.0.4_p8-r0:
#     - CVE-2022-21540
#     - CVE-2022-21541
#     - CVE-2022-21549
#     - CVE-2022-25647
#     - CVE-2022-34169
#   17.0.3_p7-r0:
#     - CVE-2022-21426
#     - CVE-2022-21449
#     - CVE-2022-21434
#     - CVE-2022-21443
#     - CVE-2022-21476
#     - CVE-2022-21496
#   17.0.2_p8-r0:
#     - CVE-2022-21291
#     - CVE-2022-21305
#     - CVE-2022-21277
#     - CVE-2022-21360
#     - CVE-2022-21365
#     - CVE-2022-21366
#     - CVE-2022-21282
#     - CVE-2022-21296
#     - CVE-2022-21299
#     - CVE-2022-21283
#     - CVE-2022-21293
#     - CVE-2022-21294
#     - CVE-2022-21340
#     - CVE-2022-21341
#     - CVE-2022-21248
#   17.0.1_p12-r0:
#     - CVE-2021-35567
#     - CVE-2021-35586
#     - CVE-2021-35564
#     - CVE-2021-35556
#     - CVE-2021-35559
#     - CVE-2021-35561
#     - CVE-2021-35578
#     - CVE-2021-35603

prepare() {
	default_prepare
	# update autoconf files to detect alpine
	update_config_sub
}

build() {
	if [ $_run_jtreg -ne 0 ]; then
		_with_jtreg="--with-jtreg=/usr/share/java/jtreg"
	else
		_with_jtreg="--with-jtreg=no"
	fi

	if [ -n "$USE_CCACHE" ]; then
		# workaround ccache being disallowed
		export PATH="/usr/bin:/bin:/sbin:/usr/sbin"
		local ccache="--enable-ccache"
	fi

	export CFLAGS="$CFLAGS -Wno-format -Wno-error=format-security"
	export CXXFLAGS="$CXXFLAGS -Wno-format -Wno-error=format-security"
	export CPPFLAGS="$CPPFLAGS -Wno-format -Wno-error=format-security"

	# we want to build hotspot with better optimisations; it's set to this
	# (prepended) anyway, and it's huge
	export CFLAGS="$CFLAGS -O3"
	export CXXFLAGS="$CXXFLAGS -O3"

	# CFLAGS, CXXFLAGS and LDFLAGS are ignored as shown by a warning
	# in the output of ./configure unless used like such:
	#  --with-extra-cflags="$CFLAGS"
	#  --with-extra-cxxflags="$CXXFLAGS"
	#  --with-extra-ldflags="$LDFLAGS"
	# See also paragraph "Configure Control Variables" from "common/doc/building.md"
	# shellcheck disable=2097 disable=2098
	CFLAGS='' CXXFLAGS='' LDFLAGS='' \
		bash ./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--target=$CTARGET \
		--prefix="$_java_home" \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--with-extra-cflags="$CFLAGS -D_LARGEFILE64_SOURCE" \
		--with-extra-cxxflags="$CXXFLAGS -D_LARGEFILE64_SOURCE" \
		--with-extra-ldflags="$LDFLAGS" \
		--with-zlib=system \
		--with-libjpeg=system \
		--with-giflib=system \
		--with-libpng=system \
		--with-lcms=system \
		--with-jobs=${JOBS:-4} \
		--with-test-jobs=${JOBS:-4} \
		--with-native-debug-symbols=none \
		--with-gtest=../googletest-1.13.0 \
		$ccache \
		$_with_jtreg \
		--disable-warnings-as-errors \
		--disable-precompiled-headers \
		--enable-dtrace=no \
		--with-jvm-variants=$_jvm_variants \
		--with-debug-level=release \
		--with-version-pre= \
		--with-version-opt="alpine-r$pkgrel" \
		--with-version-build="${pkgver##*p}" \
		--with-vendor-name="Alpine" \
		--with-vendor-url="https://alpinelinux.org/" \
		--with-vendor-bug-url="https://gitlab.alpinelinux.org/alpine/aports/issues" \
		--with-vendor-vm-bug-url="https://gitlab.alpinelinux.org/alpine/aports/issues"
	# in rare cases the build hangs
	MAKEFLAGS='' timeout 5h make jdk-image
	MAKEFLAGS='' make static-libs-image
}

check() {
	# run the gtest unittest suites
	# they don't take long, DO NOT DISABLE THEM!
	MAKEFLAGS='' make test-hotspot-gtest

	# The jtreg tests take very, very long to finish and show some failures (9 - 12 on my machine, varying between runs)
	# I think these are not critical and can be safely ignored.
	# As the tests take too long, they are disabled by default.
	# When updating this aport please let them run at least once on your machine to see if the failure count changes.
	if [ $_run_jtreg -ne 0 ]; then
		_logfile=$( mktemp -p "$builddir" )
		MAKEFLAGS='' xvfb-run make \
			run-test-tier1 \
			run-test-tier2 \
			run-test-tier3 \
			| tee "$_logfile"
		msg "---------------------------------------"
		msg "The build log can be found at $_logfile"
		# abort the build so you may take a look at the logfile
		false
		return 1
	fi
}

package() {
	local libstaticdir="lib/static/linux-${CARCH/x86_64/amd64}/musl"

	mkdir -p "$pkgdir/$_java_home/$libstaticdir"

	cd build/linux-*-$_jvm_variants-release/images/
	cp -r jdk/* "$pkgdir/$_java_home"
	cp -r static-libs/lib/* "$pkgdir/$_java_home/$libstaticdir"
}

_jmods() {
	pkgdesc="Oracle OpenJDK 17 (jmods)"
	depends=""
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot"
	mv "$_fromroot/jmods" "$_toroot"
}

_demos() {
	pkgdesc="Oracle OpenJDK 17 (demos)"
	depends=""
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot"
	mv "$_fromroot/demo" "$_toroot"
}

_doc() {
	pkgdesc="Oracle OpenJDK 17 (Documentation)"
	depends=""
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot"
	mv "$_fromroot/man" "$_toroot"
}

_jre() {
	pkgdesc="Oracle OpenJDK 17 (JRE)"
	depends="$pkgname-jre-headless"
	provides=java-jre
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot/lib"
	mv "$_fromroot/lib/libawt_xawt.so" \
		"$_fromroot/lib/libfontmanager.so" \
		"$_fromroot/lib/libjavajpeg.so" \
		"$_fromroot/lib/libjawt.so" \
		"$_fromroot/lib/libjsound.so" \
		"$_fromroot/lib/liblcms.so" \
		"$_fromroot/lib/libsplashscreen.so" \
		"$_toroot/lib"
}

_src() {
	pkgdesc="Oracle OpenJDK 17 (sources)"
	depends="$pkgname-jre-headless"
	mkdir -p "$subpkgdir/$_java_home"/lib
	mv "$pkgdir"/$_java_home/lib/src.zip \
		"$subpkgdir"/$_java_home/lib/
}

_static_libs() {
	pkgdesc="Oracle OpenJDK 17 libraries for static linking"
	provides="java-jdk-static-libs"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove $_java_home/lib/static
}

_jre_headless() {
	pkgdesc="Oracle OpenJDK 17 (JRE headless)"
	depends="java-common java-cacerts"
	provides=java-jre-headless
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot"
	mv "$_fromroot/lib" "$_toroot"

	# ct.sym should stay in -jdk
	mkdir "$_fromroot/lib"
	mv "$_toroot/lib/ct.sym" "$_fromroot/lib"

	mkdir -p "$_toroot/bin"
	for i in java \
		jfr \
		jrunscript \
		keytool \
		rmiregistry; do
		mv "$_fromroot/bin/$i" "$_toroot/bin/$i"
	done

	mv "$_fromroot/legal"              "$_toroot"
	mv "$_fromroot/conf"               "$_toroot"
	mv "$_fromroot/release"            "$_toroot"
	cp "$builddir/ASSEMBLY_EXCEPTION"  "$_toroot"
	cp "$builddir/LICENSE"             "$_toroot"
	cp "$builddir/README.md"           "$_toroot"

	# symlink to shared cacerts store
	rm "$_toroot/lib/security/cacerts"
	ln -sf /etc/ssl/certs/java/cacerts \
		"$_toroot/lib/security/cacerts"

	# symlink for java-common to work (expects jre in $_java_home/jre)
	ln -sf . "$_toroot/jre"
}

_jdk() {
	pkgdesc="Oracle OpenJDK 17 (JDK) ($_jvm_variants variant)"
	depends="$pkgname-jre $pkgname-jmods"
	provides=java-jdk
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot"
	mv "$_fromroot/bin" "$_toroot"
	mv "$_fromroot/lib" "$_toroot"
	mv "$_fromroot/include" "$_toroot"
}

sha512sums="
6f2220b3b0b9ab4441968d487858449f0e58400bfbbf8cc322b6c9393ab44869ea27285bed1022ac531f0a9eec5b7d6a07db4f7e78b094bb5ca8cedd5f40711a  jdk-17.0.13-ga.tar.gz
70c0cfb1b4147bdecb467ecb22ae5b5529eec0abc085763213a796b7cdbd81d1761d12b342060539b936fa54f345d33f060601544874d6213fdde79111fa813e  gtest-1.13.0.tar.gz
007e643039af096e77e491ccba821596c7e96b80d9821a7fbe4a78ca3f013c5e771956461d82505c26b0ca3ab06699877f398833db459e2442c0c51f71fee361  ppc64le.patch
"
